package cn.senjoeson.triper.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.adapter.DayFragmentAdapter;
import cn.senjoeson.triper.base.BaseActivity;
import cn.senjoeson.triper.base.GreenDaoManager;
import cn.senjoeson.triper.bean.Plan;
import cn.senjoeson.triper.dao.PlanDao;
import cn.senjoeson.triper.fragment.BaseTripContentFragment;

public class TripContentActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.floatButton)
    FloatingActionButton mFloatButton;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.time)
    ImageView mTime;
    @BindView(R.id.order)
    ImageView mOrder;
    @BindView(R.id.map)
    ImageView mMap;
    @BindView(R.id.left_arrow)
    ImageView mLeftArrow;

    private ArrayList<Fragment> mFragments;
    public Plan mPlan;
    private OnTimeClickListener mOnTimeClickListener;
    private OnOrderClickListener mOnOrderClickListener;
    private OnMapClickListener mOnMapClickListener;

    @Override
    protected void initView() {
        long planid = getIntent().getLongExtra("planId", -1);
        PlanDao planDao = GreenDaoManager.getInstance().getNewSession().getPlanDao();
        List<Plan> plans = planDao.queryRaw("where _id =?", String.valueOf(planid));
        mPlan = plans.get(0);
        mTitle.setText(mPlan.getTripName());
        mTitle.setTextColor(Color.WHITE);
        setSupportActionBar(mToolbar);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_trip_content;
    }

    @Override
    protected void initData() {
        DayFragmentAdapter dayFragmentAdapter = new DayFragmentAdapter(getSupportFragmentManager());
        mFragments = new ArrayList<>();
        int days = mPlan.getDays();
        for (int i = 0; i < days; i++) {
            mFragments.add(getFragment(new String[]{mPlan.getStartDate(), String.valueOf(i + 1)}));
        }

        dayFragmentAdapter.setFragments(mFragments);
        mViewPager.setAdapter(dayFragmentAdapter);
    }

    @Override
    protected void initListener() {
        mFloatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(SearchActivity.class);    //指向搜索页面
            }
        });
        mTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnTimeClickListener.onTimeClick(v);
               finish();
            }
        });
        mOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnOrderClickListener.onOrderClick(v);
            }
        });

        mMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnMapClickListener.onMapClick(v);
            }
        });
        mLeftArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
               finish();
            }
        });

    }

    public Fragment getFragment(String[] args) {
        long planid = getIntent().getLongExtra("planId", -1);
        BaseTripContentFragment fragment = new BaseTripContentFragment();
        Bundle bundle = new Bundle();
        bundle.putStringArray("argment", args);
        bundle.putLong("planId",planid);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 提供接口给fragment使用
     *
     * @param onTimeClickListener
     */
    public void setOnTimeClickListener(OnTimeClickListener onTimeClickListener) {
        if (onTimeClickListener != null) {
            this.mOnTimeClickListener = onTimeClickListener;
        }
    }

    /**
     * 提供接口给fragment使用
     *
     * @param
     */
    public void setOnOrderClickListener(OnOrderClickListener onOrderClickListener) {
        if (onOrderClickListener != null) {
            this.mOnOrderClickListener = onOrderClickListener;
        }
    }

    /**
     * 提供接口给fragment使用
     *
     * @param
     */
    public void setOnMapClickListener(OnMapClickListener onMapClickListener) {
        if (onMapClickListener != null) {
            this.mOnMapClickListener = onMapClickListener;
        }
    }


    public interface OnTimeClickListener {
        void onTimeClick(View view);
    }

    public interface OnOrderClickListener {
        void onOrderClick(View view);
    }

    public interface OnMapClickListener {
        void onMapClick(View view);
    }


}
